﻿Imports System.ComponentModel

Public Class MetaXtraDocument
    Inherits DevExpress.XtraRichEdit.RichEditControl

    <DefaultValue("(none)"), Category("MetaData")>
    Public Property QueryName As String
    <Category("MetaData")>
    Public Property QueryID As Guid
    <Category("MetaData")>
    Public Property DocumentTemplateID As Guid?
    <DefaultValue("(none)"), Category("MetaData")>
    Public Property DocumentCode As String
    <DefaultValue("(none)"), Category("MetaData")>
    Public Property DocumentName As String
    <DefaultValue("(none)"), Category("MetaData")>
    Public Property DocumentDesc As String
    <DefaultValue("(none)"), Category("MetaData")>
    Public Property DocumentDescALT As String
    <Category("MetaData")>
    Public Property CategoryId As Guid?
    <Category("MetaData")>
    Public Property ClassId As Guid?
    <Category("MetaData")>
    Public Property TypeId As Guid?

    Public Sub New()
        MyBase.New()
        Me.TypeId = IdRepository.APP_DOCTEMPLATETYPE_DOC
        Me.Options.DocumentSaveOptions.CurrentFormat = DevExpress.XtraRichEdit.DocumentFormat.Rtf
        Me.Options.DocumentSaveOptions.DefaultFormat = DevExpress.XtraRichEdit.DocumentFormat.Rtf
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="documentCode">DocumentTemplateCD</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal documentCode As String)
        Me.New()
        Me.LoadMetaDocumentByCode(documentCode)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="id">DocumentTemplateID</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal id As Guid?)
        Me.New()
        Me.LoadMetaDocument(id)
    End Sub

    Private Sub AssignProperties(ByVal dr As DataRow)
        Me.DocumentTemplateID = CType(dr("DocumentTemplateId"), Guid)
        Me.DocumentName = dr("Name").ToString
        Me.DocumentDesc = dr("Description").ToString
        Me.DocumentDescALT = dr("Description_ALT").ToString

        If Not IsDBNull(dr("DocumentTemplateCD")) Then
            Me.DocumentCode = dr("DocumentTemplateCD").ToString
        End If

        If Not IsDBNull(dr("CategoryId_FK")) Then
            Me.CategoryId = CType(dr("CategoryId_FK"), Guid?)
        End If

        If Not IsDBNull(dr("ClassId_FK")) Then
            Me.ClassId = CType(dr("ClassId_FK"), Guid?)
        End If

        If Not IsDBNull(dr("TypeId_FK")) Then
            Me.TypeId = CType(dr("TypeId_FK"), Guid?)
        End If
        If Not IsDBNull(dr("QueryID_FK")) Then
            Me.QueryID = CType(dr("QueryID_FK"), Guid)
        End If
        'Attention si le fichier n'est pas valide ca créer des exceptions au moment de faire un save as
        Me.Options.DocumentSaveOptions.DefaultFileName = "C:\" + Me.DocumentName
    End Sub
    Public Sub LoadMetaDocument(ByVal id As Guid?)
        Dim dr As DataRow
        Try
            Using mqDoc As MetaQuery = MetaQuery.GetMetaQueryByName("ReadDocFromDocTemplate")
                mqDoc.SQLWhere = String.Format("DocumentTemplateId = '{0}' ", id.ToString)
                mqDoc.FillResultQuery()
                '
                If mqDoc.ResultQuery.Rows.Count = 1 Then
                    dr = mqDoc.ResultQuery.Rows(0)
                    If Not IsDBNull(dr("DocumentData")) Then
                        Me.RtfText = dr("DocumentData").ToString
                    End If
                    AssignProperties(dr)
                End If
            End Using
        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try
    End Sub

    Public Sub LoadMetaDocumentByCode(ByVal documentCode As String)
        Dim dr As DataRow
        Try
            Using mqDoc As MetaQuery = MetaQuery.GetMetaQueryByName("ReadDocFromDocTemplate")
                mqDoc.SQLWhere = String.Format("DocumentTemplateCD = '{0}' ", documentCode)
                mqDoc.FillResultQuery()
                '
                If mqDoc.ResultQuery.Rows.Count = 1 Then
                    dr = mqDoc.ResultQuery.Rows(0)
                    If Not IsDBNull(dr("DocumentData")) Then
                        Me.RtfText = dr("DocumentData").ToString
                    End If
                    AssignProperties(dr)
                End If
            End Using

        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try
    End Sub

    Public Sub SaveMetaDocument()
        Dim sqlcmd As SqlCommand = Nothing
        Using docStream As IO.MemoryStream = New IO.MemoryStream()
            Try
                'pour le sauver dans la BD. Le Format xml semble plus petit a date - aleblanc 2012-12-16
                'report.Tag = Nothing

                Me.SaveDocument(docStream, DevExpress.XtraRichEdit.DocumentFormat.Rtf)
                docStream.Position = 0
                sqlcmd = MetaQuery.GetSPCmdByMetaName("DocumentBuilder_Save")
                sqlcmd.Parameters("@DocumentTemplateId").Value = Me.DocumentTemplateID
                sqlcmd.Parameters("@CategoryId_FK").Value = Me.CategoryId
                sqlcmd.Parameters("@TypeId_FK").Value = Me.TypeId
                sqlcmd.Parameters("@ClassId_FK").Value = Me.ClassId
                sqlcmd.Parameters("@Name").Value = Me.DocumentName
                sqlcmd.Parameters("@Description").Value = Me.DocumentDesc
                sqlcmd.Parameters("@Description_ALT").Value = Me.DocumentDescALT
                sqlcmd.Parameters("@DataFormatId_FK").Value = IdRepository.APP_DATAFORMAT_BIN
                sqlcmd.Parameters("@DocumentData").Value = Me.RtfText
                'sqlcmd.Parameters("@BinaryData").Value = docStream.ToArray
                sqlcmd.Parameters("@QueryID_FK").Value = Me.QueryID
                sqlcmd.ExecuteNonQuery()
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Using
    End Sub

End Class
